package cate.game.pvp.pointrace.handler;

import cate.common.table.d.GDFight;
import cate.game.pvp.pointrace.data.PointRaceOpponent;
import cate.game.role.pvp.PvpRecord;

public class BothPointRaceChallenge implements PointRaceChallenge {

    @Override
    public PvpRecord handleChallenge(ChallengeParam param) {
        PvpRecord record = new PvpRecord();
        PointRaceOpponent oppA = param.getOppA();
        PointRaceOpponent oppB = param.getOppB();
        record.snapshotA = oppA.getSnapshot().copy();
        record.snapshotB = oppB.getSnapshot().copy();
        record.winner = param.getWinner();
        record.valueA = oppA.getPoints();
        record.valueB = oppB.getPoints();
        // A(挑战者) 赢
        // 根据玩家当前排名 得到 基础的积分
        if (param.getWinner() == GDFight.Team.A) {
            final int pointsBaseA = 15;
            final int pointsBaseB = 10;
            long pointsBeforeA = oppA.getPoints();
            long pointsBeforeB = oppB.getPoints();
            // 附加积分
            long pointsAttach;
            long delta = Math.abs(pointsBeforeA - pointsBeforeB) / 50;
            if (delta > 5) {
                delta = 5;
            }
            pointsAttach = delta * 2;

            long chA = pointsBaseA + pointsAttach;
            long chB = pointsBaseB + pointsAttach;
            // 当玩家≥2000，＜2500时，挑战比他低的玩家，最多增加15点，对方扣5点，
            // 当玩家≥2500，＜3000时，挑战比他低的玩家，最多增加10点，对方最多扣3点，
            // 大于3000时，挑战比他低的玩家，最多增加6点，对方最多扣1点
            if (pointsBeforeA >= 2000 && pointsBeforeA < 2500 && pointsBeforeA > pointsBeforeB) {
                record.valueChangeA = Math.min(15, chA);
                record.valueChangeB = -(Math.min(5, chB));
            } else if (pointsBeforeA >= 2500
                    && pointsBeforeA < 3000
                    && pointsBeforeA > pointsBeforeB) {
                record.valueChangeA = Math.min(10, chA);
                record.valueChangeB = -(Math.min(3, chB));
            } else if (pointsBeforeA >= 3000 && pointsBeforeA > pointsBeforeB) {
                record.valueChangeA = Math.min(6, chA);
                record.valueChangeB = -(Math.min(1, chB));
            } else {
                record.valueChangeA = chA;
                record.valueChangeB = -chB;
            }
            long newSelfPoint = Math.max(1000, pointsBeforeA + record.valueChangeA);
            long newOppoPoint = Math.max(1000, pointsBeforeB + record.valueChangeB);
            record.valueA = newSelfPoint;
            record.valueB = newOppoPoint;
        }
        return record;
    }
}
